package keshe.view;

import keshe.ACCOUNT;
import keshe.MyObjectOutputStream;
import keshe.Exception.negativeException;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class toOtherJFrame extends JFrame implements ActionListener {
    private ArrayList<ACCOUNT> accounts;
    private ACCOUNT account;
    private JLabel other=new JLabel("请输入要转入的银行卡号");
    private JLabel passWord=new JLabel("请输入转出的金额");
    private JTextField textField1=new JTextField();
    private JTextField textField2=new JTextField();
    private JButton button1=new JButton();
    private JButton button2=new JButton();
    public toOtherJFrame(ACCOUNT account,ArrayList<ACCOUNT> accounts){
        this.account=account;
        System.out.println(account);
        this.accounts=accounts;
        this.setDefaultCloseOperation(2);
        //取消页面布局，按照bounds放置组件
        this.setLayout(null);
        this.setBounds(700,300,400,230);
        this.setTitle("转账界面");

        //转入的账户的标签
        other.setBounds(100,25,200,25);
        //文本行
        textField1.setBounds(100,50,200,25);

        //输入转账金额标签
        passWord.setBounds(100,75,200,25);
        //文本行
        textField2.setBounds(100,100,200,25);


        button1.setBounds(110,150,60,25);
        button1.setText("转账");
        button1.setBackground(Color.CYAN);
        button2.setBounds(230,150,60,25);
        button2.setText("取消");
        button2.setBackground(Color.yellow);
        this.add(button1);
        this.add(button2);
        this.add(passWord);
        this.add(textField2);
        this.add(textField1);
        this.add(other);
        //背景
        ImageIcon background=new ImageIcon("src/keshe/image/toOtherbackground01.jpg");
        JLabel ground=new JLabel(background);
        ground.setBounds(0,-20,400,230);
        this.add(ground);
        AddListener();

        this.setVisible(true);
    }
    //添加监听
    private void AddListener() {
        //取消就关闭页面
        button2.addActionListener(e->{
            this.dispose();
        });
        //转账
        button1.addActionListener(e->{
            String bankId=textField1.getText();
            ACCOUNT account1 = null;
            System.out.println(accounts);
            System.out.println("------------------------");
            try {
                for (int i = 0; i < accounts.size();) {
                    System.out.println(i);
                    ACCOUNT account2=accounts.get(i);
                    if(account2.equals(account)){
                        accounts.remove(i);
                        System.out.println(111);
                    }
                    if (account2.getBankCardId().equals(bankId)){
                        account1=accounts.get(i);
                        accounts.remove(i);
                    }
                }
                double money=Double.parseDouble(textField2.getText());
                if(account1==null){
                    Show("转入的账户不存在！");
                }else {
                    account.drawMoney(money);
                    account1.depositMoney(money);
                    Show("转账成功!  您的余额为："+account.getMoney());
                    accounts.add(account1);
                }
            } catch (NumberFormatException e1) {
                Show("转出的金额不符合规则");
            } catch (negativeException e1) {
                Show(e1.getMessage()+"您的余额为："+account.getMoney());
            }
            accounts.add(account);
            CINData(accounts);
            this.dispose();
        });
    }
    private void Show(String message){
        JOptionPane.showMessageDialog(this,message);
    }
    private void CINData(ArrayList<ACCOUNT> accounts) {
        MyObjectOutputStream oos= null;
        try {
            oos = new MyObjectOutputStream(new FileOutputStream("AccountData/Accounts",false));
            for (ACCOUNT account1:accounts) {
                oos.writeObject(account1);
            }
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }
}
